package com.iwhalecloud.citybrain.flow.platform.manager.application.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author: niechanggang
 * @date:2021/8/4 17:52
 * @description:
 */
@Configuration
public class CustomConfig {

    @Resource
    private RestProperties restProperties;

    @Bean
    public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(restProperties.getConnectionRequestTimeout());
        httpRequestFactory.setConnectTimeout(restProperties.getConnectTimeout());
        httpRequestFactory.setReadTimeout(restProperties.getReadTimeout());
        return httpRequestFactory;
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(customHttpRequestFactory());
    }

}
